<template>
  <div>
    <div class="box3" v-if="playlist">
      <!-- 头部导航 -->
      <div class="header">
        <div class="button" @click="$router.go(-1)">
          <span class="iconfont icon-fanhui"></span>
        </div>
        <div class="center">MV</div>
        <router-link
          tag="div"
          to="/search"
          class="classification iconfont icon-sousuo"
        ></router-link>
      </div>
      <!-- 视频 -->
      <video
        :style="{
          'object-fit': 'fill',
          top: show ? '-70%' : '-10%',
          'z-index': show ? '2003' : '0',
        }"
        ref="video"
        width="100%"
        height="200"
        :controls="show"
        :src="url"
        @click.stop="toggleplay"
        @loadeddata="loadeddataFun"
      ></video>
      <!-- 播放按钮 -->
      <div
        :style="{ top: show ? '-73%' : '-13%' }"
        @click.stop="toggleplay"
        class="playbutton iconfont icon-24gf-play"
        v-show="iconplay"
      ></div>
      <!-- 右侧分享按钮 -->
      <div class="share">
        <!-- 点赞 -->
        <div
          @click="fail"
          :class="[
            'iconfont',
            'dianz',
            fase ? 'icon-dianzan1' : 'icon-dianzan2',
          ]"
        >
          <!-- <span>10.6万</span> -->
        </div>
        <!-- 评论 -->
        <div is-link @click="showPopup" class="iconfont icon-pinglun"></div>
        <!-- 分享 -->
        <div @click="showShare = true" class="iconfont icon-fenxiang"></div>
        <!-- 收藏 -->
        <div
          @click="collection"
          :class="['iconfont', ection ? 'icon-dagou' : 'icon-jia1']"
        ></div>
      </div>
      <!-- 弹出评论 -->
      <van-popup
        :overlay="false"
        v-model="show"
        closeable
        close-icon-position="top-right"
        position="bottom"
        :style="{ height: '70%' }"
      >
        <div class="comments">
          <div class="mvcomments">
            <div class="MV">
              <span class="mv">MV</span
              ><span class="text">{{ playlist.name }}</span>
            </div>
            <div class="singer">作者：{{ playlist.artistName }}</div>
            <div class="releasetime">
              <span>发布时间：{{ playlist.publishTime }}</span>
              <span class="hengang">|</span>
              <span>播放：{{ playlist.playCount | playCountData }}</span>
            </div>
          </div>
          <p>精彩评论</p>
          <!-- 每一个评论 -->
          <div
            v-for="(item, index) in hotComments"
            :key="index"
            class="namecomments"
          >
            <div class="name">
              <!-- 用户头像 -->
              <div class="topimgs">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <!-- 用户昵称和时间 -->
              <div class="right">
                <div class="top">{{ item.user.nickname }}</div>
                <div class="bottomtime">{{ item.timeStr }}</div>
              </div>
              <div class="give">
                <span class="Liketimes">{{ item.likedCount }}</span>
                <span
                  @click="isd(item,index)"
                  :class="[
                    'iconfont',
                    'dianzan',
                    item.liked ? 'icon-dianzan1' : 'icon-dianzan2',
                  ]"
                ></span>
              </div>
            </div>
            <div class="namebox">
              {{ item.content }}
            </div>
          </div>
          <div class="inputbottom">
            <input
              @keyup.enter="commentstext"
              v-model="text"
              placeholder="用评论鼓励一下TA吧~"
              type="text"
            />
            <span class="iconfont icon-fasong"></span>
            <span @click="commentstext" class="textspan">发送</span>
          </div>
        </div>
      </van-popup>

      <!-- 进度条 -->
      <div class="range">
        <div class="bar">
          <van-slider
            inactive-color="#514E4C"
            bar-height="4px"
            active-color="#ee0a24"
            @change="sliderchange"
            v-model="value"
          >
            <template #button>
              <div class="custom-button"></div>
            </template>
          </van-slider>
        </div>
      </div>
      <!-- 下至头像 -->
      <div class="topimg">
        <div class="img">
          <img :src="playlist.cover" alt="" />
        </div>
        <div class="textname">
          <h3 class="h3">{{ playlist.artistName }}</h3>
          <p>{{ playlist.name }}</p>
        </div>
      </div>
      <!-- 分享弹出框 -->
      <van-share-sheet
        v-model="showShare"
        title="立即分享给好友"
        :options="options"
        @select="onSelect"
      />
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import {
  getMvUrl,
  getMvDetail,
  getCommentmv,
  getComment,
  getMvsub,
  getCommentLike,
} from "../../api/userinfo";
import {
  getUserDetail,
  getUserPlaylist,
  getLoginStatus,
} from "../../api/userinfo";
export default {
  data() {
    return {
      playlist: null,
      url: null,
      timeLeft: 0, //时间
      iconplay: false, //播放按钮
      value: null,
      percentage: 50,
      showShare: false,
      fase: false,
      ection: null,//判断是否收藏
      show: false,
      isdia: false,
      text: null, //发表评论
      hotComments: [], //获取评论列表
      videoId: null, //视频ID
      cid: null, //评论ID
      ID:null,//视频列表id
      lists:[],//视频列表
      options: [
        { name: "微信", icon: "wechat" },
        { name: "微博", icon: "weibo" },
        { name: "复制链接", icon: "link" },
        { name: "分享海报", icon: "poster" },
        { name: "二维码", icon: "qrcode" },
      ],
    };
  },
  methods: {
    // 播放
    toggleplay() {
      if (this.iconplay) {
        this.$refs.video.play(); //播放视频
        clearInterval(this.clearset); //清除定时器
        this.clearset = setInterval(() => {
          let currentTime = this.$refs.video.currentTime; //实时变化时间
          let duration = this.$refs.video.duration; //总时间
          this.timeLeft = currentTime;
          // 计算滚动条值
          this.value = (currentTime / duration) * 100;
          if (this.value == 100) {
            clearInterval(this.clearset);
            this.$refs.video.pause(); //暂停视频
            this.iconplay = true;
          }
        }, 999);
      } else {
        this.$refs.video.pause(); //暂停视频
        clearInterval(this.clearset);
      }
      this.iconplay = !this.iconplay;
    },
    showPopup() {
      this.show = true;
    },
    loadeddataFun() {
      this.toggleplay(); //自动播放
      let duration = this.$refs.video.duration;
      this.timeRight = duration;
    },
    // 获取滚动条变化
    sliderchange() {
      var duration = (this.$refs.video.duration * this.value) / 100;
      this.timeLeft = duration; //修改页面数据
      this.$refs.video.currentTime = duration; //修改播放时间
    },
    // 分享按钮
    onSelect(option) {
      Toast(option.name);
      this.showShare = false;
    },
    // 点赞
    fail() {
      this.fase = !this.fase;
      if (this.fase) {
        Toast({
          message: "点赞成功",
          position: "bottom",
          duration: 500,
        });
      } else {
        Toast({
          message: "取消点赞",
          position: "bottom",
          duration: 500,
        });
      }
    },
    //收藏
    collection() {
      this.ection = !this.ection;
      if (this.ection) {
        Toast({
          message: "收藏成功",
          position: "bottom",
          duration: 500,
        });
        getMvsub({ t: 1, mvid: this.videoId }).then((data) => {
          console.log(data);
        });
      } else {
        Toast({
          message: "取消收藏",
          position: "bottom",
          duration: 500,
        });
        getMvsub({ t: 0, mvid: this.videoId }).then((data) => {
          console.log(data);
        });
      }
    },
    // 判断点赞
    isd(item,index) {
      // this.isdia = !this.isdia;
      console.log(this.hotComments[index].commentId)
      if (item.liked) {
        this.$set(item, (item.liked = false));
        Toast({
          message: "取消点赞",
          position: "bottom",
          duration: 500,
        });
        // item.likedCount-=1;
        getCommentLike({ t: 0, id:this.videoId, cid: this.hotComments[index].commentId }).then((data) => {
          console.log(data);
        });
      } else {
        this.$set(item, (item.liked = true));
        Toast({
          message: "点赞成功",
          position: "bottom",
          duration: 500,
        });
        // item.likedCount+=1;
        getCommentLike({ t: 1, id:this.videoId, cid: this.hotComments[index].commentId }).then((data) => {
          console.log(data);
        });
      }
    },
    commentstext() {
      getComment({t:1,type:1, id: this.videoId, content: this.text }).then((data) => {
        console.log(data);
        this.text = "";
        Toast({
          message: "发布成功",
          position: "bottom",
          duration: 500,
        });
      });
    },
  },
  created() {
    let queryId = this.$route.query;
    this.videoId = queryId.id;
    if (!("id" in queryId)) {
      this.$router.push("/userInfo");
    }
    // 获取MV数据
    getMvDetail({ mvid: queryId.id }).then((data) => {
      // console.log(data.data);
      this.playlist = data.data;
      this.ection = data.subed
      console.log(data.subed)
    });
    // 获取mv地址
    getMvUrl({ id: queryId.id, r: 1080 }).then((data) => {
      this.url = data.data.url;
    });
    // 获取评论数据
    getCommentmv({ id: queryId.id }).then((data) => {
      console.log(data.hotComments.commentId);
      this.hotComments = data.hotComments;
      console.log(this.hotComments);
    });
      //获取用户状
      getLoginStatus().then((data) => {
        console.log(data);
        // 获取uid
        this.Id = data.data.profile.userId;
        // 获取用户详情
        getUserDetail({ uid: this.Id }).then((data) => {
          this.name = data.profile.nickname; //昵称
          this.img = data.profile.avatarUrl; //头像
          this.backgroundimg = data.profile.backgroundUrl; //背景
          this.follows = data.profile.follows; //关注
        });
        //收藏的歌单列表
        getUserPlaylist({ uid: this.Id }).then((data) => {
          console.log(data.playlist);
          this.lists = data.playlist;
          console.log(data.playlist[1].subscribed);
        });
      });
  },
  beforeDestroy() {
    clearInterval(this.clearset);
  },
  filters: {
    playCountData(value) {
      let w = value > 10000 ? value / 10000 : value; //万计算
      let y = w > 10000 ? parseInt(value / 10000) + "亿" : parseInt(w) + "万";
      return y;
    },
  },
};
</script>

<style lang="less">
.box3 {
  width: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: auto 0;
  background-color: #292929;
  overflow: hidden;
  z-index: 10;
  .header {
    width: 375px;
    height: 90px;
    // background-color: chartreuse;
    display: flex;
    position: absolute;
    z-index: 1;
    .button {
      width: 45px;
      height: 45px;
      line-height: 45px;
      text-align: center;
      position: absolute;
      left: 0;
      span {
        font-size: 25px;
        color: white;
      }
    }
    .center {
      width: 45px;
      text-align: center;
      height: 25px;
      line-height: 25px;
      color: black;
      border-radius: 40px;
      font-size: 17px;
      background-color: #eae4e4;
      margin: 0 auto;
      margin-top: 10px;
      font-weight: bold;
    }
    .classification {
      position: absolute;
      right: 0;
      width: 45px;
      height: 45px;
      text-align: center;
      line-height: 45px;
      color: white;
      font-size: 22px;
    }
  }
  video {
    position: absolute;
    top: -10%;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
  }
  .playbutton {
    width: 80px;
    height: 80px;
    color: white;
    position: absolute;
    text-align: center;
    line-height: 80px;
    font-size: 50px;
    top: -13%;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
  }
  .share {
    width: 45px;
    display: flex;
    flex-direction: column;
    position: absolute;
    right: 2%;
    top: 52%;
    .dianz {
      font-size: 30px;
    }
    div {
      width: 45px;
      height: 45px;
      color: white;
      font-size: 25px;
      text-align: center;
      line-height: 45px;
      position: relative;
      padding: 4px 0;
      // span {
      //   font-size: 12px;
      //   position: absolute;
      //   text-align: center;
      //   left: 5px;
      //   top: 25px;
      // }
    }
    .cell {
      width: 45px;
      height: 45px;
      color: white;
      font-size: 25px;
      /* text-align: center; */
      line-height: 45px;
      position: relative;
      padding: 4px 0;
      background: transparent;
    }
  }
  .comments {
    margin-bottom: 45px;
    width: 100%;
    .mvcomments {
      padding-bottom: 12px;
      div {
        padding: 10px 0px 0px 10px;
      }
      .MV {
        color: black;
        font-size: 20px;
        display: flex;
        align-items: center;
        .mv {
          font-size: 12px;
          border: 1px solid #d33a31;
          color: #d33a31;
          padding: 0 5px;
          margin-right: 10px;
        }
        .text {
          width: 250px;
          height: 24px;
          overflow: hidden;
          line-height: 24px;
        }
      }
      .singer {
        font-size: 14px;
        color: #507daf;
      }
      .releasetime {
        font-size: 14px;
        color: #999;
        .hengang {
          padding: 0 15px;
          color: #eee;
        }
      }
    }
    p {
      width: calc(100% - 10px);
      padding: 5px 0px 5px 10px;
      color: #666;
      background-color: #eee;
      font-size: 12px;
    }
    .namecomments {
      width: 100%;
      .name {
        width: 100%;
        display: flex;
        position: relative;
        .topimgs {
          padding: 10px;
          width: 35px;
          height: 35px;
          img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
          }
        }
        .right {
          display: flex;
          flex-direction: column;
          justify-content: center;
          .top {
            font-size: 14px;
            color: #666;
          }
          .bottomtime {
            font-size: 12px;
            color: #999;
            padding-top: 3px;
          }
        }
        .give {
          position: absolute;
          top: 10px;
          right: 10px;
          .Liketimes {
            font-size: 12px;
            color: #999;
            padding-right: 5px;
          }
          span {
            line-height: 12px;
          }
          .dianzan {
            font-size: 17px;
          }
        }
      }
      .namebox {
        color: #444343;
        margin-left: 54px;
        font-size: 15px;
        padding-bottom: 15px;
        margin-right: 5px;
        border-bottom: 1px solid #f1f2f3;
      }
      padding-bottom: 10px;
    }
    .inputbottom {
      position: fixed;
      width: 100%;
      bottom: 0px;
      height: 45px;
      border-top: 1px solid #fbf4f4;
      background-color: white;
      input {
        width: 75%;
        height: 45px;
        margin-left: 5%;
        border: transparent;
        color: #666;
      }
      ::-webkit-input-placeholder {
        color: #dedede;
      }
      span {
        display: inline-block;
        height: 45px;
        line-height: 45px;
        padding: 0px 5px;
      }
      .textspan {
        font-size: 14px;
        color: #999;
      }
    }
  }
  .van-popup__close-icon--top-right {
    top: 16px;
    right: 16px;
  }
  .range {
    width: 100%;
    position: absolute;
    bottom: 10%;
    .custom-button {
      width: 6px;
      height: 6px;
      background-color: #fff;
      border-radius: 50%;
    }
  }
  .topimg {
    width: 80%;
    display: flex;
    position: absolute;
    top: 77%;
    left: 3%;
    .img {
      width: 45px;
      height: 45px;
      border: 2px solid white;
      border-radius: 50%;
      img {
        width: 45px;
        height: 45px;
        border-radius: 50%;
      }
    }
    .textname {
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding-left: 10px;
      .h3 {
        font-size: 16px;
        color: white;
      }
      p {
        font-size: 12px;
        color: #ababab;
      }
    }
  }
}
</style>